//
//  CustomTagView.m
//  APP_login
//
//  Created by edz on 7/13/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "CustomTagView.h"
#import "Masonry.h"

@interface CustomTagView()

@property (nonatomic, strong) UIView *wheitView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *confirmButton;

@end

@implementation CustomTagView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.3];
        
        _wheitView = [[UIView alloc] init];
        [self addSubview:_wheitView];
        [_wheitView mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.right.mas_equalTo(0);
            make.bottom.mas_equalTo(90);
            make.height.mas_equalTo(90);
        }];
        _wheitView.backgroundColor = [UIColor whiteColor];
        
        _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_wheitView addSubview:_cancelButton];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(20);
            make.size.mas_equalTo(CGSizeMake(60, 30));
        }];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_cancelButton addTarget:self action:@selector(cancelCustomTag) forControlEvents:UIControlEventTouchUpInside];
        
        _confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_wheitView addSubview:_confirmButton];
        [_confirmButton mas_makeConstraints:^(MASConstraintMaker *make){
           make.top.mas_equalTo(0);
           make.right.mas_equalTo(-20);
           make.size.mas_equalTo(CGSizeMake(60, 30));
        }];
        [_confirmButton setTitle:@"确认" forState:UIControlStateNormal];
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_confirmButton addTarget:self action:@selector(confirmCustomTag) forControlEvents:UIControlEventTouchUpInside];
        
        _textField = [[UITextField alloc] init];
        [_wheitView addSubview:_textField];
        [_textField mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(20);
            make.width.mas_equalTo(380);
            make.top.mas_equalTo(40);
            make.height.mas_equalTo(40);
        }];
        
        _textField.placeholder = @"请自定义标签";
        _textField.font = [UIFont systemFontOfSize:20];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        _textField.keyboardType = UIKeyboardTypeDefault;
        [_textField becomeFirstResponder];
        _textField.delegate = (id)self;
    }
    return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.text.length >10 ) {
        return NO;
    }else{
        return YES;
    }
    return nil;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hidden];
}

- (void)cancelCustomTag{
    [self hidden];
}

- (void)confirmCustomTag{
    if (self.delegent && [self.delegent respondsToSelector:@selector(customTagViewDelegentGetTagTitle:)]) {
        [self.delegent customTagViewDelegentGetTagTitle:_textField.text];
    }
    [self hidden];
}

- (void) hidden{
    [UIView animateWithDuration:0.3 delay:0.5
         options:UIViewAnimationOptionCurveLinear animations:^{
        [self.wheitView mas_updateConstraints:^(MASConstraintMaker *make){
            make.bottom.mas_equalTo(90);
        }];
        [self layoutIfNeeded];
         }
         completion:^(BOOL finished){
                [self removeFromSuperview];
          }
    ];
}

- (void) showView:(UIView*) view{
    [view addSubview:self];
    [UIView animateWithDuration:0.3 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.wheitView mas_updateConstraints:^(MASConstraintMaker *make){
            make.bottom.mas_equalTo(-350);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finish){
        
    }];
}
@end
